package com.gk.study.service.impl;


import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.study.entity.Guest;
import com.gk.study.entity.User;
import com.gk.study.mapper.GuestMapper;
import com.gk.study.mapper.UserMapper;
import com.gk.study.service.GuestService;
import com.gk.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class GuestServiceImpl extends ServiceImpl<GuestMapper, Guest> implements GuestService {
    @Autowired
    private GuestMapper guestMapper;
   public static final String KEY = "guest:code";
    /**
     * 查询住户信息
     * @param keyword
     * @return
     */
    @Override
    public List<Guest> getGuestList(String keyword) {
        List<Guest> list = null;
        try {
            list = guestMapper.list(keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 新增
     * @param guest
     */
    @Override
    public void createGuest(Guest guest) {
        guestMapper.createGuest(guest);
    }

    /**
     * 修改
     * @param guest
     */
    public void updateGuest(Guest guest){
        guestMapper.updateGuest(guest);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteUser(String id) {
        guestMapper.deleteById(id);
    }

    @Override
    public Guest getGuestByPhone(String phone) {
        return guestMapper.getGuestByPhone(phone);
    }

    @Override
    public Guest getGuestOne(Guest guest) {
        QueryWrapper<Guest> queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", guest.getPhone());
        queryWrapper.eq("password", guest.getPassword());
        return guestMapper.selectOne(queryWrapper);

    }

    @Override
    public Guest getGuestDetail(int id) {
        QueryWrapper<Guest> queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        return guestMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateGuestPartial(Guest guest) {
            // 创建 UpdateWrapper 对象
            UpdateWrapper<Guest> updateWrapper = new UpdateWrapper<>();
            // 设置更新条件，例如根据 ID 更新
            updateWrapper.eq("id", guest.getId());
            // 更新的字段
            if (guest.getName() != null) {
                updateWrapper.set("name", guest.getName());
            }
            if (guest.getSex() != null) {
                updateWrapper.set("sex", guest.getSex());
            }
            if (guest.getPhone() != null) {
                updateWrapper.set("phone", guest.getPhone());
            }
            if (guest.getEnterTime() != null) {
                updateWrapper.set("enter_time", guest.getEnterTime());
            }
            if (guest.getHType() != 0) { // assuming 0 is not a valid value
                updateWrapper.set("h_type", guest.getHType());
            }
            if (guest.getNum() != null) {
                updateWrapper.set("num", guest.getNum());
            }
            if (guest.getPassword() != null) {
                updateWrapper.set("password", guest.getPassword());
            }
            if (guest.getAddress() != null) {
                updateWrapper.set("address", guest.getAddress());
            }
            if (guest.getDescription() != null) {
                updateWrapper.set("description", guest.getDescription());
            }
            if (guest.getEmail() != null) {
                updateWrapper.set("email", guest.getEmail());
            }
            if (guest.getAvatar() != null) {
                updateWrapper.set("avatar", guest.getAvatar());
            }
            // 执行更新操作
            guestMapper.update(guest, updateWrapper);
        }
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void sendCode(String phone) {
        //先查询是否存在对应的手机号码
        Guest guest = guestMapper.getGuestByPhone(phone);
        String guestPhone = guest.getPhone();
        if (!guestPhone.isEmpty()){
            //生成验证码,
            String code = new RandomUtil().randomNumbers(6);
            System.out.println(code);
            //将验证码和电话号码一起存进redis中
            stringRedisTemplate.opsForValue().set(KEY+phone,code,2, TimeUnit.MINUTES);
        }
    }

    @Override
    public String verifyCode(String phone, String code) {
        //取出验证码
        String guestCode = stringRedisTemplate.opsForValue().get(KEY + phone);
        if (guestCode.isEmpty()){
            return null;
        }
        code = guestCode;
        return code;
    }

    @Override
    public Guest findByName(String name) {
        return guestMapper.findByName(name);
    }
}
